Option Explicit On
Option Strict On

Imports System.Collections.Generic
Imports TreeCreeper01Interfaces


Friend Class cSpatialPlantMap

    Private pLocations As List(Of ISpatialPlantMapLocation)
    Private pCount As Integer
    Private pMaximumXCoordinate As Double
    Private pMaximumYCoordinate As Double
    Private pMinimumXCoordinate As Double
    Private pMinimumYCoordinate As Double
    Private pSpatialPLantMapLocationDistances As cSpatialPlantMapLocationDistances


    Public Sub New(ByVal SuggestedPlantCapacity As Integer)
        pLocations = New List(Of ISpatialPlantMapLocation)
        pLocations.Capacity = SuggestedPlantCapacity
    End Sub

    Public Function AddNewPlantLocation(ByVal xCoordinate As Double, ByVal yCoordinate As Double) As cSpatialPlantMapLocation

        Dim NewLocation As cSpatialPlantMapLocation

        pCount = pCount + 1

        NewLocation = New cSpatialPlantMapLocation(pCount, xCoordinate, yCoordinate)

        ' calculate the max/min coordinates for viewport
        If pCount = 1 Then
            pMaximumXCoordinate = xCoordinate
            pMaximumYCoordinate = yCoordinate
            pMinimumXCoordinate = xCoordinate
            pMinimumYCoordinate = yCoordinate
        Else
            If xCoordinate < pMinimumXCoordinate Then
                pMinimumXCoordinate = xCoordinate
            End If
            If xCoordinate > pMaximumXCoordinate Then
                pMaximumXCoordinate = xCoordinate
            End If
            If yCoordinate < pMinimumYCoordinate Then
                pMinimumYCoordinate = yCoordinate
            End If
            If yCoordinate > pMaximumYCoordinate Then
                pMaximumYCoordinate = yCoordinate
            End If
        End If

        pLocations.Add(NewLocation)

        Return NewLocation

    End Function

    Public ReadOnly Property Count() As Integer
        Get
            Count = pCount
        End Get
    End Property

    Public ReadOnly Property LocationByIndexBase1(ByVal Index As Integer) As ISpatialPlantMapLocation
        Get
            If Index <= pLocations.Count Then
                Return pLocations.Item(Index - 1)
            End If
            Return Nothing
        End Get
    End Property

    Friend Sub CalculateInterdistancesIfNeeded()

        pSpatialPLantMapLocationDistances = New cSpatialPlantMapLocationDistances(pLocations)

    End Sub

    Public Function GetInterdistanceBetweenTwoLocations(ByVal LocationOne As ISpatialPlantMapLocation, ByVal LocationTwo As ISpatialPlantMapLocation) As Double

        Return pSpatialPLantMapLocationDistances.GetInterdistanceBetweenTwoLocations(LocationOne, LocationTwo)

    End Function

    ReadOnly Property MaximumHorizontalOrVerticalSpatialSeparation() As Double
        Get
            Dim MaxSeparationX As Double
            Dim MaxSeparationY As Double
            Dim MaxSeparationBigger As Double

            MaxSeparationX = pMaximumXCoordinate - pMinimumXCoordinate
            MaxSeparationY = pMaximumYCoordinate - pMinimumYCoordinate

            If MaxSeparationX > MaxSeparationY Then
                MaxSeparationBigger = MaxSeparationX
            Else
                MaxSeparationBigger = MaxSeparationY
            End If

            Return MaxSeparationBigger

        End Get
    End Property



End Class

